<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller { 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in()){	redirect('auth/login');	}		
		$this->load->library('form_validation');
		$this->load->model('founder/user_model');
	}
   
   function add_user()
   {
	$meta['page_title'] = "User";
	$meta['message'] = "Add New User";
	
	$data['roles'] =  $this->ion_auth_model->groups()->result();
	$data['departments'] = $this->ion_auth_model->departments()->result();
	$data['user_types'] = $this->ion_auth_model->user_type()->result();

	$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
	$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
	$this->form_validation->set_rules('email', 'Email',  'required|valid_email|is_unique[users.email]');
	$this->form_validation->set_rules('phone', 'Phone',  'required');
	$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
	$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
	$this->form_validation->set_rules('role', 'Role', 'required');
	//$this->form_validation->set_rules('user_type', 'User Type', 'required');
	//$this->form_validation->set_rules('department', 'Department', 'required');
	

if ($this->form_validation->run() == FALSE)
		{
		  $meta['message'] = "Error";
		  $this->load->view('common/header', $meta);
	      $this->load->view('founder/add-user',$data);
    	  $this->load->view('common/footer');
		}
		else
		{
			$name = strtolower($this->input->post('first_name'))." ".strtolower($this->input->post('last_name'));
			
			$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
			$email    = $this->input->post('email');
			$password = $this->input->post('password');
			
			$additional_data = array(
				'first_name' => $this->input->post('first_name'),
				'last_name'  => $this->input->post('last_name'),				
				'phone'      => $this->input->post('phone'),
			);
			
			//insert into database	
			$group = $this->input->post('role');
			
			$this->ion_auth->register($username, $password, $email, $additional_data,$group);
			
	  redirect("founder/user/get_all_user");
	  
		}
 		
   }
   
   function get_all_user()
   {
	   //list the users
		$this->data['users'] = $this->user_model->get_employees();
		$this->data['founders'] = $this->user_model->get_founders();
		$this->data['clients'] = $this->user_model->get_clients();
			
			//print_r($this->data['users']);exit;
		foreach ($this->data['users'] as $k => $user)
		{
			$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
		}
	
	 foreach ($this->data['users'] as $l => $user)
			{
				$this->data['users'][$l]->user_type = $this->ion_auth->get_user_type($user->id)->result();
			}
			
			foreach ($this->data['users'] as $l => $user)
			{
				$this->data['users'][$l]->department = $this->ion_auth->get_user_department($user->id)->result();
			}
			
			
			
			// for founders
		foreach ($this->data['founders'] as $f => $founder)
			{
				$this->data['founders'][$f]->groups = $this->ion_auth->get_users_groups($founder->id)->result();
			}
	
	 foreach ($this->data['founders'] as $g => $user)
			{
				$this->data['founders'][$g]->user_type = $this->ion_auth->get_user_type($founder->id)->result();
			}
			
			foreach ($this->data['founders'] as $h => $user)
			{
				$this->data['founders'][$h]->department = $this->ion_auth->get_user_department($founder->id)->result();
			}
	 
	// $this->data['user_type'] =  $this->ion_auth_model->get_user_type($this->session->userdata('user_id'))->row();
	  //echo $get_user_type->name;
	   
	  // exit;
	   $meta['page_title'] = "User List";
	   $this->data['message'] = "User Created Successfully";
	 //  $this->session->set_flashdata('message', 'User Created Successfully');
	   $this->load->view('common/header', $meta);
	   $this->load->view('founder/view-user',$this->data);
       $this->load->view('common/footer');
	   
   }
   
   function get_employess(){
   	 $q = $this->user_model->get_employees();
	//echo '<pre>';
	//echo 'asd';
	print_r($q);
   }
   
   function user_type()
   {
	   
	   $data['user_types'] = $this->ion_auth_model->user_type()->result();
	   
	   $meta['page_title'] = "Create User Type";
	   $this->load->view('common/header', $meta);
	   $this->load->view('founder/user-type',$data);
       $this->load->view('common/footer');
	   	   
	}
	
	function create_user_type()
	{
		$name = $_REQUEST['name'];
	   	$desc = $_REQUEST['desc'];
	   
	   $insert = array(
	   'name' => $name,
	   'description' => $desc
	   );
	   	   
	   $this->user_model->create_user_type($insert);  
	}
	
   
   function department()
   {
	  $data['departments'] = $this->ion_auth_model->departments()->result();
	   
	   $meta['page_title'] = "Create User Type";
	   
	   $this->load->view('common/header', $meta);
	   $this->load->view('founder/department',$data);
       $this->load->view('common/footer');
   }
   
   function create_department()
   {
	   $name = $_REQUEST['name'];
	   $desc = $_REQUEST['desc'];
	   
	   $insert = array(
	   'name' => $name,
	   'description' => $desc
	   );
	   	   
	   $this->user_model->create_department($insert); 
   }
   
   function edit_department($id)
   {   	
	$access = array('founder','superadmin');
	if($this->ion_auth->in_group($access))
	{		
   	$meta['page_title'] = 'Edit Department';
	$data['department'] = $this->user_model->get_department_by_id($id);	
	
	$this->form_validation->set_rules('name', 'Name', 'required|xss_clean');
	$this->form_validation->set_rules('desc', 'Description', 'required|xss_clean');
	
	if($this->form_validation->run()==TRUE){
		$update = array(	
		'name'			=> strtolower($this->input->post('name')),
		'description'	=> $this->input->post('desc'),
		);
		
		$id = array(
		'id' 			=> $this->input->post('id'),
		);
		
		//print_r($update);
		//exit;
					
		$this->user_model->update_department($id,$update);
		$this->session->set_flashdata('msg','<i class="icon-ok-sign"></i>  Department Updated successfully');
		redirect('founder/user/edit_department/'.$this->input->post('id'));
	}
	
	$this->load->view('common/header', $meta);
	$this->load->view('founder/edit-department',$data);
    $this->load->view('common/footer');
	
	}
	else{
		redirect('founder/user/department');
	}
   }
   
   function user_wizard()
   {
	   $meta['page_title'] = "User Wizard";
	   
	   $this->load->view('common/header', $meta);
	   $this->load->view('founder/user-wizard');
       $this->load->view('common/footer');
   }
    
}
	
	?>